/*******************************************************************************
 *
 *    Copyright (c) Baina Info Tech Co. Ltd
 *
 *    My_Jamendo_music
 *    
 *    MusicTabActivity
 *    TODO File description or class description.
 *
 *    @author: zuochen
 *    @since:  2011-1-27
 *    @version: 1.0
 *
 ******************************************************************************/

package com.mp3.dwonloader.ringtonemaker.musicradio.version.zl.activity;

import android.app.TabActivity;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Process;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TextView;

import com.mp3.dwonloader.ringtonemaker.musicradio.version.zl.R;
import com.mp3.dwonloader.ringtonemaker.musicradio.version.zl.service.DownloadService;
import com.mp3.dwonloader.ringtonemaker.musicradio.version.zl.service.PlayerService;

/**
 * MusicTabActivity of My_Jamendo_music.
 * 
 * @author zuochen
 * 
 */

public class MusicTabActivity extends TabActivity {
	private static final String TAB_MUSIC = "Music";
	private static final String TAB_RINGTONE = "Ringtone";
	private TabHost tabHost;

	public static final int MSG_MIN = 202;
	public static final int MSG_EXIT = 203;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setVolumeControlStream(AudioManager.STREAM_MUSIC);
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		tabHost = getTabHost();
		final View tabLocal = View.inflate(this, R.layout.tab_indicator, null);
		((TextView) tabLocal.findViewById(R.id.title)).setText(TAB_MUSIC);
		tabLocal.setBackgroundResource(R.drawable.tab_local_bk);
		tabHost.addTab(tabHost.newTabSpec(TAB_MUSIC).setIndicator(tabLocal)
				.setContent(new Intent(this, HomeActivity.class)));

		final View tabOnline = View.inflate(this, R.layout.tab_indicator, null);
		((TextView) tabOnline.findViewById(R.id.title)).setText(TAB_RINGTONE);
		tabOnline.setBackgroundResource(R.drawable.tab_online_bk);
		tabHost.addTab(tabHost.newTabSpec(TAB_RINGTONE).setIndicator(tabOnline)
				.setContent(new Intent(this, Start.class)));
		// Recommend mRecommend = new Recommend(this);
		// mRecommend.showCommend(this, 1);
	}

	private final Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == MSG_EXIT) {
				final PlayerService mPlayerService = new PlayerService();
				mPlayerService.displayNotifycation();
				try {
					stopService(new Intent(MusicTabActivity.this,
							PlayerService.class));
					stopService(new Intent(MusicTabActivity.this,
							DownloadService.class));
				} catch (final Exception e) {
				}

				Process.killProcess(Process.myPid());
			} else if (msg.what == MSG_MIN) {
				finish();
			}
		}

	};

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (KeyEvent.KEYCODE_BACK == event.getKeyCode()
				&& event.getAction() == KeyEvent.ACTION_DOWN) {
			if (tabHost.getCurrentTabTag().equals(TAB_RINGTONE)) {
				tabHost.setCurrentTabByTag(TAB_MUSIC);
				return true;
			}

		}
		return super.dispatchKeyEvent(event);
	}

}
